<?php

/*
class Component, base component class
Copyright (C) 2011  Petr Čermák

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Checker.php';
require_once 'Application.php';

abstract class Component{

    private $enabled = true;
    protected $owner = null;
    private $name = null;
    private $id = null;

    /**Initializes the component.*/
    public abstract function initialize();

    /**Processes the request from user.*/
    public abstract function processRequest();

    /**
     * Creates the response for user and converts it to string.
     * @return string  the created response
     */
    public function createOutput(){
        if(!$this->isEnabled())return "";
        ob_start();
        $this->processRequest();
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }

    /**
     * Gets name of the component.
     * @return string component name
     */
    public function getName(){return $this->name;}

    /**
     * Sets owner of the component.
     * @param Container the component owner
     */
    public function setOwner(Container $owner){$this->owner = $owner;}

    /**
     * Gets owner of the component.
     * @return Container the component owner
     */
    public function getOwner(){return $this->owner;}

    /**
     * Sets name of the component.
     * @param string component name
     */
    public function setName($name){
        Checker::checkString($name);
        $this->name = $name;
    }

    /**
     * Sets whether this component will be shown inside a container.
     * @param bool value
     */
    public function setEnabled($enabled){
        Checker::checkBool($enabled);
        $this->enabled = $enabled;
    }

    /**
     * Checks if this component will be shown inside a container.
     * @return bool value
     */
    public function isEnabled(){
        return $this->enabled;
    }

    /**
     * Creates unique identification string for this component.
     * @return bool value
     */
    protected function createUniquePathId(){
        $string = '';
        $comp = $this;
        $owner = $this->getOwner();
        while($owner !== null){
            $string .= '_'.$comp->getName();
            $comp = $owner;
            $owner = $comp->getOwner();
        }
        return sha1($string);
    }

    /**
     * Creates unique identification hash for this component.
     * @return bool value
     */
    protected function createUniqueHashId(){
        return sha1($this->createUniquePathId());
    }

}
?>
